﻿using System;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Comprehensive
{
    public class FeedbackRequestedRule : ComprehensiveRule<RPCompanyProfile>
    {
        public override Severity Severity => Severity.Yellow;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.Comprehensive;
        public override string Code => "YR5";
        public override string InformationalMessage => "All companies in Comprehensive framework must have feedback requested checkbox checked";

        public FeedbackRequestedRule()
        {
            For(x => x.LastFeedbackRequestDate).When(Validator).WithKey(x => x.Id).WithMessage("The company {0} #{1}# does not have compliant feedback");
        }

        private bool Validator(RPCompanyProfile validatedCompanyProfile, RPCompanyProfile referenceCompanyProfile)
        {
            //This rule will be applied only for companies with LastFullReviewDate older than 3 months
            if (validatedCompanyProfile.LastFeedbackRequestDate.Date == DateTime.MinValue)
                return false;

            if (validatedCompanyProfile.LastFullReviewDate.Date.AddMonths(3) < DateTime.UtcNow.Date)
                return true;

            return validatedCompanyProfile.LastFeedbackRequestDate.Date.AddMonths(3) >= DateTime.UtcNow.Date;
        }
    }
}
